﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StorageBox
{
    public class DispositionManager : ManagerBase
    {
        private static DispositionManager _dispositionManager;

        private string FilePath = Directory.GetCurrentDirectory();

        private string FileName = @"Disposition.json";

        private string path;

        public DispositionFile _disposition { get; private set; }

        public DispositionManager()
        {
            _dispositionManager = this;

            path = this.FilePath + '/' + this.FileName;
            if (!IsExist())
                CreateDisposition();
            LoadDisposition();
        }

        #region Write
        public void AddApplyData(ApplyData data)
        {
            _disposition.AppltDataLists.Add(data);
        }
        #endregion

        private bool IsExist()
        {
            return File.Exists(path);
        }

        private void CreateDisposition()
        {
            File.Create(path).Close();
            _disposition = new DispositionFile();
            WriteDispoition();
        }

        public void LoadDisposition()
        {
            using (StreamReader sr = new StreamReader(path))
            {
                var json = sr.ReadToEnd();
                _disposition = JsonConvert.DeserializeObject<DispositionFile>(json)!;
            }

            if (_disposition == null)
            {
                _disposition = new();
            }
        }
        public void WriteDispoition()
        {
            var json = JsonConvert.SerializeObject(_disposition);
            using (StreamWriter sw = new(path))
            {
                sw.Write(json);
            }
        }
    }
}
